#include "strategy.h"

using namespace std;
int endmodule = 0;
int advantagenum = 0;

//  get distance^2 from center of House
float get_dist(float x, float y)
{
	return pow(x - TEE_X, 2) + pow(y - TEE_Y, 2);
}

//  get distance^2 of two cordinates
float get_dist(float x1, float y1, float x2, float y2)
{
	return pow(x1 - x2, 2) + pow(y1 - y2, 2);
}

bool is_in_House(float x, float y)
{
	if (get_dist(x, y) < pow(HOUSE_R + STONE_R, 2)) {
		return true;
	}
	else {
		return false;
	}
}

bool is_in_Core(float x, float y)
{
	if (get_dist(x, y) < pow(Core_R + STONE_R, 2)) {
		return true;
	}
	else {
		return false;
	}
}

bool is_in_HC(float x, float y)
{
	if (get_dist(x, y) < pow(HC_R + STONE_R, 2)) {
		return true;
	}
	else {
		return false;
	}
}

int number_in_house(const GAMESTATE* const gs)
{
int num1 = 0, num2 = 0, i;
for (i = 0; i < gs->ShotNum; i++)
{
	if (is_in_House(gs->body[i][0], gs->body[i][1]) == 1 && i % 2 == gs->ShotNum % 2) num1++;
	if (is_in_House(gs->body[i][0], gs->body[i][1]) == 1 && i % 2 != gs->ShotNum % 2) num2++;
}
if (num1 > num2) return 1;
else return 0;
}

int number_in_defend(const GAMESTATE* const gs)
{
	int num = 0, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][1] > TEE_Y + 4.88 && gs->body[i][0]<TEE_X + 0.8 && gs->body[i][0]>TEE_X - 0.8)num++;
	}
	return num;
}

int number_in_left(const GAMESTATE* const gs)
{
int num = 0, i;
for (i = 0; i < gs->ShotNum; i++)
{
	if (gs->body[i][1] > TEE_Y && gs->body[i][0] < TEE_X)num++;
}
return num;
}

int number_in_right(const GAMESTATE* const gs)
{
	int num = 0, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][1] > TEE_Y && gs->body[i][0] > TEE_X)num++;
	}
	return num;
}

void get_ranking(int* rank, const GAMESTATE* const gs)
{
	// init array
	for (int i = 0; i < 16; i++) {
		rank[i] = i;
	}
	// sort
	int tmp;
	for (int i = 0; i < gs->ShotNum - 1; i++) {
		for (int j = 0; j < gs->ShotNum - 1 - i; j++) {
			if (get_dist(gs->body[rank[j]][0], gs->body[rank[j]][1]) > get_dist(gs->body[rank[j + 1]][0], gs->body[rank[j + 1]][1])) {
				// swap
				tmp = rank[j];
				rank[j] = rank[j + 1];
				rank[j + 1] = tmp;
			}
		}
	}
}

bool getxianhou(const GAMESTATE* const gs)
{
	if (gs->ShotNum % 2 == 0) return 1;
	else return 0;
}

int whoinhouse(const GAMESTATE* const gs, int rank[])
{
	int housetag = 0;
	if (is_in_House(gs->body[rank[0]][0], gs->body[rank[0]][1]))
	{
		//printf("\n%d\n", rank[0]);
		if (rank[0] % 2 != gs->ShotNum % 2) {
			housetag = -1;
		}
		else {
			housetag = 1;
		}
	}
	return housetag;
}

int counttheirclosernum(const GAMESTATE* const gs, int rank[])
{
	int ourclose, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (rank[i] % 2 == gs->ShotNum % 2)
		{
			ourclose = rank[i];
			break;
		}
	}
	return i;
	printf("theirclose:%d\n", i);
}

int judgeobstacle(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	int tag = 0, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] > y + 0.3)
		{
			tag++;
		}
	}
	return tag;
}

int judgeobstacle2(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y, float y2)
{
	int tag = 0, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] > y && gs->body[i][1] < y2)
		{
			tag++;
		}
	}
	return tag;
}

int judgeobstacle3(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	int tag = 0, i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] < y - 0.3)
		{
			if (i % 2 == gs->ShotNum % 2)
			{
				tag = 1;
				printf("obstacle&ours\n");
				break;
			}
			else
			{
				tag = -1;
				printf("obstacle&theirs\n");
				break;
			}
		}
	}
	return tag;
}

int findobstacle(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	int i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] > y + 0.3) break;
	}
	printf("obstacle：%f , %f\n", gs->body[i][0], gs->body[i][1]);
	return i;
}

bool judgeprotect(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
int tag = 0, i;
for (i = 0; i < gs->ShotNum; i++)
{
	if (gs->body[i][0]<x + 0.2 && gs->body[i][0]>x - 0.2 && gs->body[i][1] >= y + 0.3)
	{
		tag = 1;
		break;
	}
}
return tag;
}

bool judgelrnum(const GAMESTATE* const gs, float x, float y)
{
	int i, tag = 1, num1 = 0, num2 = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0] > x + 0.1 && gs->body[i][0]<x + 1.5 && gs->body[i][1] > y + 0.1) num1++;
	}
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0] < x - 0.1 && gs->body[i][0]>x - 1.5 && gs->body[i][1] > y + 0.1) num2++;
	}
	if (num1 < num2) tag = 0;
	return tag;
}

int judgelnum(const GAMESTATE* const gs, float x, float y)
{
	int i, num = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0] < x && gs->body[i][0]>x - 0.5 && gs->body[i][1] > y + 0.3 && gs->body[i][1] < y + 2.5)  num++;
		if (gs->body[i][0] < x - 0.3 && gs->body[i][0]>x - 1.0 && gs->body[i][1] >= y + 2.5) num++;
	}
	return num;
}

int judgernum(const GAMESTATE* const gs, float x, float y)
{
	int i, num = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0] > x && gs->body[i][0]<x + 0.5 && gs->body[i][1]>y + 0.3 && gs->body[i][1] < y + 2.5)  num++;
		if (gs->body[i][0] > x + 0.3 && gs->body[i][0] < x + 1.0 && gs->body[i][1] >= y + 2.5) num++;
	}
	return num;
}

float countobstacledistance(const GAMESTATE* const gs, float x, float y)
{
	int i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] > y + 0.3) break;
	}
	printf("distance : %f\n", gs->body[i][1] - y);
	return (gs->body[i][1] - y);
}


void attack1(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	printf("rotate\n");
	vec_ret->speed = 5.4 + 0.2 * (4.88 - y);
	vec_ret->h_x = -1.21 + x - TEE_X;
	vec_ret->angle = 10;
}

void attack2(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	printf("rotate\n");
	vec_ret->speed = 5.4 + 0.2 * (4.88 - y);
	vec_ret->h_x = 1.25 + x - TEE_X;
	vec_ret->angle = -10;
}

void attack3(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x)
{
	vec_ret->speed = 7.1f;
	vec_ret->h_x = x - TEE_X;
	vec_ret->angle = 0.0f;
}

void attack5(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	printf("indirect\n");
	vec_ret->speed = 6.75f;
	vec_ret->h_x = x - TEE_X + 0.019;
	vec_ret->angle = 0.0f;
}

void attack6(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	printf("push\n");
	vec_ret->speed = 4.67 - 0.094 * (10.33 - y);
	vec_ret->h_x = x - TEE_X + 0.02;
	vec_ret->angle = 0.0f;
}

void attack4(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	int i;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (gs->body[i][0]<x + 0.3 && gs->body[i][0]>x - 0.3 && gs->body[i][1] > y + 0.3) break;
	}
	if ((x * 1.0 - gs->body[i][0]) > -0.2 && (x * 1.0 - gs->body[i][0]) < 0.2)
	{
		if (i % 2 == gs->ShotNum % 2)
		{
			attack5(gs, vec_ret, gs->body[i][0], gs->body[i][1]);
		}
		else
		{
			attack6(gs, vec_ret, gs->body[i][0], gs->body[i][1]);
		}
	}
	else
	{
		printf("pianzhuang\n");
		vec_ret->speed = 5.6f;
		if (gs->body[i][0] < x)
		{
			vec_ret->h_x = gs->body[i][0] + 0.31 - TEE_X;
		}
		else
		{
			vec_ret->h_x = gs->body[i][0] - 0.31 - TEE_X;
			vec_ret->angle = 0.0f;
		}
	}
}

void attack(const GAMESTATE* gs, SHOTINFO* vec_ret, float x, float y)
{
	if (judgeobstacle(gs, vec_ret, x, y) == 0)
	{
		printf("no obstacle\n");
		if (judgeobstacle3(gs, vec_ret, x, y) == 1)
		{
			if (judgelnum(gs, x, y) < 2 && judgelnum(gs, x, y) <= judgernum(gs, x, y) && -1.4 + x - TEE_X >= -2.23)
			{
				attack1(gs, vec_ret, x, y);
			}
			else if (judgernum(gs, x, y) < 2 && judgernum(gs, x, y) < judgelnum(gs, x, y) && 1.4 + x - TEE_X < 2.23)
			{
				attack2(gs, vec_ret, x, y);
			}
			else
			{
				attack3(gs, vec_ret, x);
			}
		}
			else if (judgeobstacle3(gs, vec_ret, x, y) == -1)
			{
				attack6(gs, vec_ret, x, y);
			}
			else
			{
				attack3(gs, vec_ret, x);
			}
		
	}
	else if (judgeobstacle(gs, vec_ret, x, y) == 1)
	{
		printf("1 obstacle\n");
		if (countobstacledistance(gs, x, y) < 2.5)  //3.74
		{
			attack4(gs, vec_ret, x, y);
		}
		else
		{
			if (judgelnum(gs, x, y) < 2 && judgelnum(gs, x, y) <= judgernum(gs, x, y) && -1.4 + x - TEE_X >= -2.23)
			{
				attack1(gs, vec_ret, x, y);
			}
			else if (judgernum(gs, x, y) < 2 && judgernum(gs, x, y) < judgelnum(gs, x, y) && 1.4 + x - TEE_X < 2.23)
			{
				attack2(gs, vec_ret, x, y);
			}
			else
			{
				attack4(gs, vec_ret, x, y);
			}
		}
	}
	else
	{
		printf("a lot obstacles\n");
		if (judgelnum(gs, x, y) < 3 && judgelnum(gs, x, y) <= judgernum(gs, x, y) && -1.4 + x - TEE_X >= -2.23)
		{
			attack1(gs, vec_ret, x, y);
		}
		else if (judgernum(gs, x, y) < 3 && judgernum(gs, x, y) < judgelnum(gs, x, y) && 1.4 + x - TEE_X < 2.23)
		{
			attack2(gs, vec_ret, x, y);
		}
		else
		{
			attack3(gs, vec_ret, x);
		}
		}
	}


		void attack7(const GAMESTATE* const gs, SHOTINFO * vec_ret, float x, float y)
	{
			if (y < 7.47)
			{
				vec_ret->speed = 3.8;
				vec_ret->h_x = x - TEE_X + 0.03;
				vec_ret->angle = 0.0f;
			}
			else
			{
				vec_ret->speed = 3.8 + 0.2 * (y - 7.47);
				vec_ret->h_x = x - TEE_X + 0.03;
				vec_ret->angle = 0.0f;
			}
	}

	void occupy1(const GAMESTATE* const gs, SHOTINFO * vec_ret)
	{
		vec_ret->speed = 2.988f;
		vec_ret->h_x = -0.02f;
		vec_ret->angle = 0.0f;
	}

	void occupy2(const GAMESTATE* const gs, SHOTINFO * vec_ret)
	{
		vec_ret->speed = 3.068f;
	vec_ret->h_x = -1.64f;
	vec_ret->angle = 10.0f;
	if (judgelrnum(gs, TEE_X, TEE_Y) == 0)
	{
		vec_ret->h_x = 1.72f;
		vec_ret->angle = -10.0f;
	}
    }

void occupy3(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	vec_ret->speed = 3.35f;
	vec_ret->h_x = -1.53f;
	vec_ret->angle = 10.0f;
	if (judgelrnum(gs, TEE_X, TEE_Y) == 0)
	{
		vec_ret->h_x = 1.55f;
		vec_ret->angle = -10.0f;
	}
}

void occupy4(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	vec_ret->speed = 3.4 + 0.3 * judgeobstacle2(gs, vec_ret, TEE_X, 4.58, 5.6);
	vec_ret->h_x = 0.0f;
	vec_ret->angle = 0.0f;
}

void occupy5(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	vec_ret->speed = 3.1 - advantagenum * 0.06;
	vec_ret->h_x = -1.75 + advantagenum * 0.1;
	vec_ret->angle = 10.0 - advantagenum * 2.0;
	if (judgelrnum(gs, TEE_X, TEE_Y) == 0)
	{
		vec_ret->h_x = 1.75 - advantagenum * 0.1;
		vec_ret->angle = -10.0 + advantagenum * 0.2;
	}
}

void occupy6(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	vec_ret->speed = 3.06f;
	vec_ret->h_x = -1.65f;
	vec_ret->angle = 10.0f;
	if (judgelrnum(gs, TEE_X, TEE_Y) == 0)
	{
		vec_ret->h_x = 1.74f;
		vec_ret->angle = -10.0f;
	}
}

void occupy(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	if (judgeobstacle(gs, vec_ret, TEE_X, 4.435) == 0)
	{
		printf("to center\n");
		occupy1(gs, vec_ret);
	}
	else
	{
		if (judgeobstacle(gs, vec_ret, TEE_X, 5.6) == 0)
		{
			printf("to center&harder\n");
			occupy4(gs, vec_ret);
		}
		else
		{
			if (judgeobstacle2(gs, vec_ret, TEE_X, 5.6, 6.9) > 0)
			{
				printf("a little behind\n");
				occupy6(gs, vec_ret);
			}
			else
			{
				if (is_in_Core(gs->body[rank[0]][0], gs->body[rank[0]][1]))
				{
					printf("rotate to center&harder\n");
					occupy3(gs, vec_ret);
				}
				else
				{
					printf("rotate to center\n");
					occupy2(gs, vec_ret);
				}
			}
		}
	}
}


void defend1(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	printf("occupy mid\n");
	vec_ret->speed = 2.7f;
	vec_ret->h_x = -0.01f;
	vec_ret->angle = 0.0f;
}

void defend2(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	vec_ret->speed = 2.56 - 0.08 * (y - 4.88);
	vec_ret->h_x = x - TEE_X + 0.02;
	vec_ret->angle = 0.0f;
}

void defend3(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	vec_ret->speed = 2.78f;
	vec_ret->h_x = -0.03f;
	vec_ret->angle = 0.0f;
}

void defend4(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	vec_ret->speed = 2.78 - 0.08 * (y - 4.88);
	vec_ret->h_x = x - TEE_X + 0.015;
	vec_ret->angle = 0.0f;
}

void defend5(const GAMESTATE* const gs, SHOTINFO* vec_ret, float x, float y)
{
	vec_ret->speed = 2.88 - 0.08 * (y - 4.88);
	vec_ret->h_x = x - TEE_X + 0.01;
	vec_ret->angle = 0.0f;
}

void advantage(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	int i, theirclose = -1, k = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (rank[i] % 2 != gs->ShotNum % 2 && is_in_House(gs->body[rank[i]][0], gs->body[rank[i]][1]) && (gs->body[rank[i]][0] > gs->body[rank[0]][0] + 0.31 || gs->body[rank[i]][0] < gs->body[rank[0]][0] - 0.31))
		{
			theirclose = rank[i];
			break;
		}
		if (rank[i] % 2 != gs->ShotNum % 2)
		{
			k++;
			if (k == 2) break;
		}
	}
	if (theirclose != -1)
	{

		attack(gs, vec_ret, gs->body[theirclose][0], gs->body[theirclose][1]);

	}
	else
	{
		occupy(gs, vec_ret, rank);
	}
		//advantagenum++;
		//if (advantagenum == 3) advantagenum = 0;
}


void advantage2(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	int i, theirclose = -1, k = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (rank[i] % 2 != gs->ShotNum % 2 && is_in_House(gs->body[rank[i]][0], gs->body[rank[i]][1]) && (gs->body[rank[i]][0] > gs->body[rank[0]][0] + 0.31 || gs->body[rank[i]][0] < gs->body[rank[0]][0] - 0.31) && judgeobstacle(gs, vec_ret, gs->body[rank[i]][0], gs->body[rank[i]][1]) == 0 && judgeobstacle3(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]) != 1)
		{
			theirclose = rank[i];
			break;
		}
		if (rank[i] % 2 != gs->ShotNum % 2)
		{
			k++;
			if (k == 1) break;
		}
	}
	if (theirclose != -1)
	{
		attack(gs, vec_ret, gs->body[theirclose][0], gs->body[theirclose][1]);
	}
	else
	{
		occupy(gs, vec_ret, rank);
	}
}
void advantage3(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	int i, tag = 0;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (i % 2 == gs->ShotNum % 2 && gs->body[i][0] != 0 && gs->body[i][1] != 0 && judgeobstacle2(gs, vec_ret, gs->body[i][0], TEE_Y, 10) == 0)
		{
			attack7(gs, vec_ret, gs->body[i][0], gs->body[i][1]);
			tag = 1;
			break;
		}
	}
	if (tag != 1)
	{
		occupy(gs, vec_ret, rank);
	}
}

void superattack(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	printf("superattack\n");
	int i, target = -1;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (rank[i] % 2 != gs->ShotNum % 2)
		{
			target = rank[i];
			break;
		}
	}
	if (target != -1)
	{
		if (gs->body[target][0] == 0 && gs->body[target][1] == 0)
		{
			occupy(gs, vec_ret, rank);
		}
		else
		{
			attack(gs, vec_ret, gs->body[target][0], gs->body[target][1]);
		}
	}
	else occupy(gs, vec_ret, rank);
}

void superattack2(const GAMESTATE* const gs, SHOTINFO* vec_ret, int rank[])
{
	printf("superattack2\n");
	int i, target = -1;
	for (i = 0; i < gs->ShotNum; i++)
	{
		if (rank[i] % 2 != gs->ShotNum % 2)
		{
			target = rank[i];
			break;
		}
	}
	if (target != -1)
	{
		if (gs->body[target][0] == 0 && gs->body[target][1] == 0)
		{
			if (judgeobstacle(gs, vec_ret, TEE_X, 5.213) == 0)
			{
				defend1(gs, vec_ret);
			}
			else
			{
				occupy(gs, vec_ret, rank);
			}
		}
		else
		{
			if (is_in_House(gs->body[target][0], gs->body[target][1]) == 1)
			{
				attack(gs, vec_ret, gs->body[target][0], gs->body[target][1]);
			}
			else
			{
				attack6(gs, vec_ret, gs->body[target][0], gs->body[target][1]);
			}
		}
	}
	else
	{
		if (judgeobstacle(gs, vec_ret, TEE_X, 5.213) == 0)
		{
			defend1(gs, vec_ret);
		}
		else
		{
			occupy(gs, vec_ret, rank);
		}
	}
}

void getBestShot(const GAMESTATE* const gs, SHOTINFO* vec_ret)
{
	int rank[16];
	get_ranking(rank, gs);
	if (getxianhou(gs))
	{
		if (gs->ShotNum == 0)
		{
			defend1(gs, vec_ret);
		}
		else if (gs->ShotNum == 2 || gs->ShotNum == 4)
		{
			superattack2(gs, vec_ret, rank);
		}
		else if (gs->ShotNum == 14)
		{
			printf("14th\n");
			if (whoinhouse(gs, rank) == 1)
			{
				if (is_in_HC(gs->body[rank[0]][0], gs->body[rank[0]][1]))
				{
					if (judgeobstacle(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]) > 0)
					{
						advantage(gs, vec_ret, rank);
					}
					else
					{
						defend5(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
					}
					//printf("\n%d   %f\n",rank[0], gs->body[rank[0]][0]);
				}
				else
				{
					if (judgeobstacle(gs, vec_ret, TEE_X, 4.88) == 0)
					{
						defend3(gs, vec_ret);
					}
					else if (judgeobstacle(gs, vec_ret, TEE_X, 4.88) == 1)
					{
						int abc = findobstacle(gs, vec_ret, TEE_X, 4.88);
						if (abc % 2 == gs->ShotNum % 2)
						{
							attack7(gs, vec_ret, gs->body[abc][0], gs->body[abc][1]);
						}
						else
						{
							advantage(gs, vec_ret, rank);
						}
					}
					{
						advantage(gs, vec_ret, rank);
					}
				}
			}
			else if (whoinhouse(gs, rank) == -1)
			{
				attack(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
			}
			else
			{
				if (judgeobstacle(gs, vec_ret, TEE_X, 4.88) == 0)
				{
					defend3(gs, vec_ret);
				}
				else if (judgeobstacle(gs, vec_ret, TEE_X, 4.88) == 1)
				{
					int abc = findobstacle(gs, vec_ret, TEE_X, 4.88);
					if (abc % 2 == gs->ShotNum % 2)
					{
						attack7(gs, vec_ret, gs->body[abc][0], gs->body[abc][1]);
					}
					else
					{
						advantage(gs, vec_ret, rank);
					}
				}
				else
				{
					advantage(gs, vec_ret, rank);
				}
			}
		}
		else
		{
			if (whoinhouse(gs, rank) == 1)
			{
				superattack(gs, vec_ret, rank);
			}
			else if (whoinhouse(gs, rank) == -1)
			{
				attack(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
			}
			else
			{
				superattack;
			}
		}
	}

	else
	{
		if (gs->ShotNum == 15)
		{
			printf("15th\n");
			if (whoinhouse(gs, rank) == 1)
			{
				advantage2(gs, vec_ret, rank);
			}
			else if (whoinhouse(gs, rank) == -1)
			{
				if (counttheirclosernum(gs, rank) == 1 && number_in_house(gs) == 1)
				{
					printf("1 closer&ourmore\n");
					attack(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
				}
				else
				{
					printf("a lot closer\n");
					if (judgeobstacle(gs, vec_ret, TEE_X, 4.435) == 0)
					{
						occupy1(gs, vec_ret);
					}
					else
					{
						if (judgeobstacle(gs, vec_ret, TEE_X, 5.6) == 0)
						{
							occupy4(gs, vec_ret);
						}
						else
						{
							if (number_in_left(gs) < 2 && number_in_left(gs) <= number_in_right(gs))
							{
								if (judgeobstacle2(gs, vec_ret, TEE_X, 5.6, 6.9) > 0)
								{
									occupy6(gs, vec_ret);
								}
								else
								{
									if (is_in_Core(gs->body[rank[0]][0], gs->body[rank[0]][1]) == 1 && gs->body[rank[0]][0] < TEE_X)
									{
										occupy3(gs, vec_ret);
									}
									else
									{
										occupy2(gs, vec_ret);
									}
								}
							}
							else if (number_in_right(gs) < 2 && number_in_right(gs) < number_in_left(gs))
							{
								if (judgeobstacle2(gs, vec_ret, TEE_X, 5.6, 6.9) > 0)
								{
									occupy6(gs, vec_ret);
								}
								else
								{
									if (is_in_Core(gs->body[rank[0]][0], gs->body[rank[0]][1]) == 1 && gs->body[rank[0]][0] > TEE_X)
									{
										occupy3(gs, vec_ret);
									}
									else
									{
										occupy2(gs, vec_ret);
									}
								}
							}
							else
							{
								attack(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
							}
						}
					}
				}
			}
			else
			{
				advantage3(gs, vec_ret, rank);
				//occupy(gs, vec_ret, rank);
			}
		}
		else if (gs->ShotNum == 1 || gs->ShotNum == 3)
		{
			superattack2(gs, vec_ret, rank);
		}
		else
		{
			if (whoinhouse(gs, rank) == 1)
			{
				if (is_in_HC(gs->body[rank[0]][0], gs->body[rank[0]][1]))
				{
					if (judgeprotect(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]) == 1)
					{
						advantage(gs, vec_ret, rank);
					}
					else
					{
						defend4(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
					}
					//printf("\n%d   %f\n",rank[0], gs->body[rank[0]][0]);
				}
				else
				{
					advantage(gs, vec_ret, rank);
				}
			}
			else if (whoinhouse(gs, rank) == -1)
			{
				attack(gs, vec_ret, gs->body[rank[0]][0], gs->body[rank[0]][1]);
			}
			else
			{
				defend1(gs, vec_ret);
			}
		}
	}
}